<?php

class CashesController extends AppController {

    var $name = 'Cashes';
    var $uses = array('Cash', 'User', 'Employee', 'Provider', 'CashMovement');

    function beforeFilter() {
        parent::beforeFilter();
        // Limito que si es un usuario del tipo user no pueda acceder
        $accion = $this->action;
        if ($this->Auth->user('group_id') == GROUP_USER && !in_array($accion, array('view'))) {
            $this->Session->setFlash(__('Acceso Restringido', true));
            $this->redirect(array('controller' => 'futures', 'action' => 'index'));
        }
    }
/*
    function index() {
        $this->Cash->recursive = 0;
        $this->set('cashes', $this->paginate());
    }
*/
    function por_dia() {

        $filtro = '1 = 1';
        $fecha_max = '';
        $fecha_min = '';
        if (!empty($this->data)) {

            $datos = $this->data['Cash'];
            if (isset($datos['date_from']) && trim($datos['date_from']) != '') {
                list($d, $m, $a) = explode("/", $datos['date_from']);
                $datos['date_from'] = $a . "-" . $m . "-" . $d . ' 00:00:00';

                $filtro .= ' AND Cash.date_open >= "' . $datos['date_from'] . '"';
                $fecha_min = 'si';
            }
            if (isset($datos['date_to']) && trim($datos['date_to']) != '') {
                list($d, $m, $a) = explode("/", $datos['date_to']);
                $datos['date_to'] = $a . "-" . $m . "-" . $d . ' 23:59:59';

                $filtro .= ' AND Cash.date_open <= "' . $datos['date_to'] . '"';
                $fecha_max = 'si';
            }
        }

        if ($fecha_max != 'si' || $fecha_min != 'si') {
            //si no cargo alguna de las dos fechas, le voy a poner que vea de 
            //los últimos 5 días
            $fecha_from = date('Y-m-d 00:00:00', strtotime('-5day'));
            $fecha_to = date('Y-m-d 23:59:59');
            
            $this->data['Cash']['date_from'] = date('d/m/Y', strtotime($fecha_from));
            $this->data['Cash']['date_to'] = date('d/m/Y', strtotime($fecha_to));
            
            $filtro .= ' AND Cash.date_open >= "' . $fecha_from . '"';
            $filtro .= ' AND Cash.date_open <= "' . $fecha_to . '"';
        }

        $cashes = $this->Cash->find('all', array(
            'recursive' => -1,
            'conditions' => array(
                $filtro
            ),
            'order' => array(
                'date_open' => 'ASC'
            )
                ));

        $array_todo = array();
        foreach ($cashes as &$cash) {

            $res = $this->CashMovement->find('first', array(
                'recursive' => -1,
                'fields' => array('SUM(CASE WHEN TYPE = "in" THEN amount ELSE 0 END) entradas', 'SUM(CASE WHEN TYPE = "out" THEN amount ELSE 0 END) salidas'),
                'conditions' => array(
                    'CashMovement.cash_id' => $cash['Cash']['id']
                ),
                'group' => array('CashMovement.cash_id')
                    ));

            $array_todo['fecha'][] = date('d/m/Y', strtotime($cash['Cash']['date_open']));
            $array_todo['monto_inicial'][] = $cash['Cash']['open_amount'];
            $array_todo['entradas'][] = $res[0]['entradas'];
            $array_todo['salidas'][] = $res[0]['salidas'];
            $array_todo['monto_final'][] = $cash['Cash']['actual_amount'];
            $array_todo['id'][] = $cash['Cash']['id'];
        }
//        
//        pr($array_todo);
//        die;

        $this->set('cashes', $array_todo);
    }

    function view($id = null) {

        $actual = 'no';
        $cash = $this->Cash->getActual();
        if (!$id) {
            //si no viene el $id de una caja particular, vamos a mostrar la caja actual
            $id = $cash['Cash']['id'];
            $actual = 'si';
        }elseif($id == $cash['Cash']['id']){
            $actual = 'si';
        }
        $this->set('cash', $this->Cash->read(null, $id));
        
        $users = $this->User->find('list');
        $employees = $this->Employee->getListName();
        $providers = $this->Provider->find('list');
        $this->set(compact('users', 'employees', 'providers', 'actual'));
    }

    /*
      function add() {
      if (!empty($this->data)) {
      $this->Cash->create();
      if ($this->Cash->save($this->data)) {
      $this->Session->setFlash(__('The cash has been saved', true));
      $this->redirect(array('action' => 'index'));
      } else {
      $this->Session->setFlash(__('The cash could not be saved. Please, try again.', true));
      }
      }
      $users = $this->Cash->User->find('list');
      $this->set(compact('users'));
      }

      function edit($id = null) {
      if (!$id && empty($this->data)) {
      $this->Session->setFlash(__('Invalid cash', true));
      $this->redirect(array('action' => 'index'));
      }
      if (!empty($this->data)) {
      if ($this->Cash->save($this->data)) {
      $this->Session->setFlash(__('The cash has been saved', true));
      $this->redirect(array('action' => 'index'));
      } else {
      $this->Session->setFlash(__('The cash could not be saved. Please, try again.', true));
      }
      }
      if (empty($this->data)) {
      $this->data = $this->Cash->read(null, $id);
      }
      $users = $this->Cash->User->find('list');
      $this->set(compact('users'));
      }

      function delete($id = null) {
      if (!$id) {
      $this->Session->setFlash(__('Invalid id for cash', true));
      $this->redirect(array('action' => 'index'));
      }
      if ($this->Cash->delete($id)) {
      $this->Session->setFlash(__('Cash deleted', true));
      $this->redirect(array('action' => 'index'));
      }
      $this->Session->setFlash(__('Cash was not deleted', true));
      $this->redirect(array('action' => 'index'));
      }
     */
}
